<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

defined('_JEXEC') or die('Restricted access'); 
$row = $this->row;

JFilterOutput::objectHTMLSafe( $row, ENT_QUOTES, 'description' );
$cparams = JComponentHelper::getParams ('com_media');

JHTML::_('behavior.modal');
?>
<script language="javascript" type="text/javascript">
function submitbutton(pressbutton) {
	var form = document.adminForm;
	if (pressbutton == 'cancel') {
		submitform( pressbutton );
		return;
	}
	if (pressbutton == 'save') {
		
	    <?php if( JRequest::getVar("task") != "edit" ){  ?>
	    if( ! checkNumber(form.coupons_count.value) ){
	        alert("Coupons count is required and must be numberic value");
	        form.coupons_count.focus();
	        return false;
	    }
	    
	    if( ! checkNumber(form.coupon_usage_count.value) ){
	        alert("Coupon usage count is required and must be numberic value");
	        form.coupon_usage_count.focus();
	        return false;
	    }
	    if( ! checkNumber(form.code_length.value) ){
	        alert("Code length is required and must be numberic value");
	        form.code_length.focus();
	        return false;
	    }
	    if( ! checkNumber(form.member_coupon_usage_count.value) ){
	        alert("Member coupon usage count is required and must be numberic value");
	        form.member_coupon_usage_count.focus();
	        return false;
	    }
	    
	    if( parseInt(form.coupon_usage_count.value) < parseInt(form.member_coupon_usage_count.value) ){
	    	alert("Coupon usage count must be greater than Member coupon usage count");
	        form.member_coupon_usage_count.focus();
	        return false;
	    }
	    <?php } ?>
	    
	    if( ! checkNumber(form.member_coupon_usage_count.value) ){
	        alert("Member coupon usage count is required and must be numberic value");
	        form.member_coupon_usage_count.focus();
	        return false;
	    }
	    	    
	    if( ! checkNumber(form.discount.value) ){
	        alert("Payment discount is required and must be numberic value");
	        form.discount.focus();
	        return false;
	    }
	    
	    if ( form.name.value == "" ) {
			alert("Coupon name is required");
			form.name.focus();
			return;
		}
        
		submitform(pressbutton);		
	}
	else {		
		submitform(pressbutton);
	}
}

// Check numberic value
function checkNumber(value){    
	var numericExpression = /^[0-9]+$/;
	var numericExpression1 = /^[0-9]+\.[0-9]{1,2}$/;
	
	if ( value.match(numericExpression) || value.match(numericExpression1) )
	   return true
	else return false;
}

function changeDateRange(checked){
	document.adminForm.begin_date.disabled = checked;
	document.adminForm.end_date.disabled = checked;
}

window.addEvent('domready', function() {
    if(document.adminForm.limit_date.checked){
		document.adminForm.begin_date.disabled = true;
		document.adminForm.end_date.disabled = true;
	}
});
</script>
	
<form name="adminForm" action="index.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
	<table width="100%">
	<tr>
	
		<td width="80%" valign="top" align="left">
		
		    <div style="width:100%;">
				<fieldset class="adminform">
					<legend><?php echo JText::_( 'Coupon detail' ); ?></legend>
						<table class="admintable">
						
						<tr>
							<td class="key">
								<label for="name" width="100">
									<?php echo JText::_( 'Coupon Name' ); ?>:					
								</label>
							</td>
							<td colspan="2">
								<input class="text_area" type="text" name="name" id="name" value="<?php echo $row->name; ?>" size="50" maxlength="50" />
								<font color="Red">*</font>
							</td>
						</tr>
						<?php if( JRequest::getVar("task") != "edit" ){  ?>
						<tr>
							<td class="key">
								<label for="coupons_count" width="100">
									<?php echo JText::_( 'Coupons Count' ); ?>:					
								</label>
								<br />
								<small>
									<?php echo JText::_('How many coupons need to be generated');?>
								</small>
							</td>
							<td colspan="2">
								<input class="text_area" type="text" name="coupons_count" id="coupons_count" value="<?php echo $row->coupons_count; ?>" size="10" maxlength="50" />
								<font color="Red">*</font>
							</td>
						</tr>
						
						<tr>
							<td class="key">
								<label for="coupon_usage_count" width="100">
									<?php echo JText::_( 'Coupons Usage Count' ); ?>:					
								</label>
								<br />
								<small>
									<?php echo JText::_('How much times a coupon key can be used, enter big value (ex.:99999) for unlimited');?>
								</small>
							</td>
							<td colspan="2">
								<input class="text_area" type="text" name="coupon_usage_count" id="coupon_usage_count" value="<?php echo $row->coupon_usage_count; ?>" size="10" maxlength="50" />
								<font color="Red">*</font>
							</td>
						</tr>
						
						<tr>
							<td class="key">
								<label for="code_length" width="100">
									<?php echo JText::_( 'Code Length' ); ?>:					
								</label>
								<br />
								<small>
									<?php echo JText::_('Generated coupon code length');?>
								</small>
							</td>
							<td colspan="2">
								<input class="text_area" type="text" name="code_length" id="code_length" value="5" size="10" maxlength="50" />
								<font color="Red">*</font>
							</td>
						</tr>
						<?php } ?>
						<tr>
							<td class="key">
								<label for="member_coupon_usage_count" width="100">
									<?php echo JText::_( 'Coupons Usage Count per Member' ); ?>:					
								</label>
								<br />
								<small>
									<?php echo JText::_('How much times coupon can be used by member');?>
								</small>
							</td>
							<td colspan="2">
								<input class="text_area" type="text" name="member_coupon_usage_count" id="member_coupon_usage_count" value="<?php echo $row->member_coupon_usage_count; ?>" size="10" maxlength="50" />
								<font color="Red">*</font>
							</td>
						</tr>
						
						<tr>
							<td class="key">
								<label for="discount" width="100">
									<?php echo JText::_( 'Discount' ); ?>:					
								</label>
								<br />
								<small>
									<?php echo JText::_('Payment discount');?>
								</small>
							</td>
							<td colspan="2">
								<input class="text_area" type="text" name="discount" id="discount" value="<?php echo $row->discount; ?>" size="5" maxlength="50" />
								<select name="discount_type">
								    <option value="%" <?php if($row->discount_type == "%") echo "selected='selected'" ?>>%</option>
								    <option value="currency" <?php if($row->discount_type == "currency") echo "selected='selected'" ?>>Currency unit</option>
								</select>
								<font color="Red">*</font>
							</td>
						</tr>
												
						<tr>
							<td valign="top" class="key">
								<label for="limit_date">
									<?php echo JText::_( 'Dates' ); ?>:	
								</label>
								<br />
								<small><?php echo JText::_('Date range when coupon can be used');?></small>
							</td>
							<td>
								<input onchange="changeDateRange(this.checked);" type="checkbox" <?php if($row->limit_date == 0) echo "checked='checked'"; ?> value="0" name="limit_date" id="limit_date"/><?php echo JText::_("Don't limit date of usage");?><br/>
								From: <?php echo JHTML::_('calendar', $row->begin_date, 'begin_date', 'begin_date', "%Y-%m-%d %H:%I:%S" ); ?> - To: <?php echo JHTML::_('calendar', $row->end_date, 'end_date', 'end_date', "%Y-%m-%d %H:%I:%S" ); ?>
							</td>
						</tr>
												
						<tr>
							<td class="key">
								<label for="posting_plan" width="100">
									<?php echo JText::_( 'Posting Plan' ); ?>:					
								</label>
								<br />
								<small>
									<?php echo JText::_('Coupons can be used with selected Posting plan<br/> If nothing selected, coupon can be used with ANY Posting plan');?>
								</small>
							</td>
							<td colspan="2">
								<?php
								    $db =& JFactory::getDBO();
								    
								    $course = array();
								    
								    $db->setQuery("SELECT * FROM #__ja_credits WHERE published = 1 ORDER BY title ASC");
								    $result = $db->loadObjectList();								    
								    foreach ( $result as $rows ){
                                        $course[] = JHTML::_('select.option', "1".$rows->id, $rows->title);
								    }
								    
								    $db->setQuery("SELECT * FROM #__ja_posting_plans WHERE published = 1 ORDER BY type ASC, title ASC");
								    $result = $db->loadObjectList();								    
								    foreach ( $result as $rows ){
                                        $course[] = JHTML::_('select.option', "2".$rows->id, $rows->title);
								    }
								    
								    if( !empty($row->posting_plan ) )
								        $row->posting_plan = explode(",", $row->posting_plan);
								    
								    echo JHTML::_('select.genericlist', $course, 'posting_plan[]', 'class="inputbox" multiple="" style="width:350px; height:100px;" ', 'value', 'text', $row->posting_plan );
								    
								?>
							</td>
						</tr>
						
						<tr>
							<td class="key">
								<label for="description" width="100">
									<?php echo JText::_( 'Description' ); ?>:					
								</label>
							</td>
							<td colspan="2">
								<textarea id="description" name="description" rows="5" cols="50"><?php echo $row->description; ?></textarea>
							</td>
						</tr>
								
						<tr>
							<td width="120" class="key">
								<?php echo JText::_( 'Published' ); ?>:						</td>
							<td>
								<?php
								$published = ($row->id) ? $row->published : 1;
								$lists['published'] = JHTML::_('select.booleanlist',  'published', 'class="inputbox"', $published );
								 echo $lists['published']; ?>						
							</td>
						</tr>
						
					</table>
				</fieldset>
			</div>
		</td>
	</tr>
	</table>
	<div class="clr"></div>	
	<input type="hidden" name="option" value="<?php echo $this->option; ?>" />
	<input type="hidden" name="view" value="jacoupon" />	
	<input type="hidden" name="task" value="" />
	<?php echo JHTML::_( 'form.token' ); ?>	
</form>
<style type="text/css">
	table.admintable td.key{
		width:300px;
	}								
</style>